package com.xoms.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.admin.model.AccountBean;
import com.xoms.admin.service.DeptService;
import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.ListResult;
import com.xoms.admin.controller.res.ObjectResult;
import com.xoms.admin.controller.res.bean.RDepartmentBean;
import com.xoms.admin.controller.springmvc.req.DelDepartmentParam;
import com.xoms.admin.controller.springmvc.req.DisableDepartmentParam;
import com.xoms.admin.controller.springmvc.req.EditDepartmentParam;
import com.xoms.admin.controller.springmvc.req.EnableDepartmentParam;
import com.xoms.admin.controller.springmvc.req.GetDeptsParam;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;

@Component
public class DeptController {

	@Autowired
	DeptService deptServiceImpl;
	
	/**
	 * 新建编辑部门
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RDepartmentBean> edit(EditDepartmentParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RDepartmentBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

			int did = NumUtil.toInt(param.getDid(), -1);// 为-1则新建
			String name = param.getName();// 部门名称
			int parentId = NumUtil.toInt(param.getParentId(), 0);
			String desc = param.getDesc();// 描述
			int isEnable = NumUtil.toInt(param.getIsEnable(), 1);// 默认启用
			
			//成功后返回编辑后部门信息
			RDepartmentBean data = deptServiceImpl.edit(account, did, name, parentId, desc, isEnable);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取部门列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RDepartmentBean> getDepts(GetDeptsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RDepartmentBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			
			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int pagesize = NumUtil.toInt(param.getPageSize(), 20);
			// 获取部门列表
			PageResult<RDepartmentBean> resData = deptServiceImpl.getDepts(account, key, page, pagesize);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 启用部门
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult enable(EnableDepartmentParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int did = NumUtil.toInt(param.getId(), -1);;// 要启用部门id

			boolean flag = deptServiceImpl.enable(account, did);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 禁用部门
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult disable(DisableDepartmentParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int did = NumUtil.toInt(param.getId(), -1);// 要禁用部门id

			boolean flag = deptServiceImpl.disable(account, did);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 删除部门
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult del(DelDepartmentParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int did = NumUtil.toInt(param.getDeptId(), -1);// 要删除的部门id

			boolean flag = deptServiceImpl.del(account, did);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

}
